/**
 * 获取o点的时间戳
 * 默认当天时间
 */
function dateFormat(date = new Date().toLocaleDateString()) {
  return new Date(date).getTime();
}

/**
 * 计算天数
 * @param {*} startDate
 * @param {*} endDate
 */
function calculateDays(startDate, endDate) {
  return parseInt((endDate - startDate) / (1000 * 60 * 60 * 24));
}

/**
 * 在一起的天数
 * @param {*} startDate 恋爱开始的时间，格式：yyyy-MM-dd
 */
function togetherDays(startDate) {
  startDate = new Date(startDate + " 00:00:00").getTime();

  const nowDate = dateFormat();

  return calculateDays(startDate, nowDate);
}

/**
 * 距离她（他）的生日天数
 * @param {*} birthday 格式：yyyy-MM-dd
 */
function getBirthday(birthday) {
  const nowYear = new Date().getFullYear();
  birthday = nowYear + "-" + birthday + " 00:00:00";

  const birthdayTime = new Date(birthday).getTime();

  const nowTime = dateFormat();

  if (nowTime <= birthdayTime) {
    return calculateDays(nowTime, birthdayTime);
  }

  let nextBirthday = calculateDays(birthdayTime, nowTime) + 365;

  if (nowYear / 4 == 0) {
    // 闰年366天
    nextBirthday++;
  }
  // 平年365天
  return nextBirthday;
}

/**
 * 随机十六进制颜色
 * @returns
 */
function getColor() {
  //十六进制颜色随机
  var r = Math.floor(Math.random() * 256);
  var g = Math.floor(Math.random() * 256);
  var b = Math.floor(Math.random() * 256);
  var color =
    "#" +
    (
      Array(6).join(0) +
      (r.toString(16) + g.toString(16) + b.toString(16))
    ).slice(-6);
  return color;
}

/**
 * 导出
 */
module.exports = {
  togetherDays,
  getBirthday,
  getColor,
};
